#!/usr/bin/env python
# encoding: utf-8
"""
...
"""

#
#  Copyright (c) 2009 University of Dundee. All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#  SUCH DAMAGE.

import unittest
import sys

from StringIO import StringIO
from getopt import getopt, GetoptError
from copy import deepcopy
from xsltbasic import XsltBasic
# We're using lxml's ElementTree implementation for XML manipulation due to
# its XSLT integration.
from lxml.etree import XML, XSLT, Element, SubElement, ElementTree, dump, parse

# Handle Python 2.5 built-in ElementTree
#try:
#        from xml.etree.ElementTree import XML, Element, SubElement, ElementTree, dump
#except ImportError:
#        from elementtree.ElementTree import XML, Element, SubElement, ElementTree, dump


def usage(error):
    """Prints usage so that we don't have to. :)"""
    cmd = sys.argv[0]
    print """%s
Usage: %s <stylesheet.xsl> <input.xml> <output.xml>
Runs the stylesheet transform upon an XML instance document.

Options:

Examples:
  %s foo.xslt bar.xml bar_new.xml

Report bugs to ome-devel@lists.openmicroscopy.org.uk""" % (error, cmd, cmd)
    sys.exit(2)

if __name__ == '__main__':

    xslt_filename = sys.argv[1]
    xslt_file = open(xslt_filename)
    in_filename = sys.argv[2]
    in_file = open(in_filename)
    try:
#        print "Running XSLT %s on %s saving to %s..." % (xslt_filename, in_filename, out_filename)
        xslt_doc = parse(xslt_file)
        in_doc = parse(in_file)
        transform = XSLT(xslt_doc)
        print transform(in_doc)

    finally:
        xslt_file.close()
        in_file.close()
